package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberBusiness2Binding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.ui.fragment.BusinessCollectingFragment;
import com.sgb.member.ui.fragment.BusinessEnterpriseFragment;
import com.sgb.member.ui.fragment.BusinessGoodsFragment;
import com.sgb.member.ui.fragment.BusinessLeaseFragment;
import com.sgb.member.ui.fragment.BusinessLogisticsFragment;
import com.sgb.member.ui.fragment.BusinessTeamFragment;
import com.sgb.member.ui.fragment.BusinessTenderFragment;
import com.sgb.member.ui.fragment.MemberBusinessFragment;
import com.sgb.member.view_model.MemberBusinessModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.MToast;
import com.swgk.core.view.countmenu.CountMenuEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 会员经营
 */
public class MemberBusinessActivity extends AppActivity {

    @Inject
    MemberBusinessModel mModel;
    private ActivityMemberBusiness2Binding mBinding;

    private final String[] mTitles = {"经营商品", "招采经营", "集采经营", "租赁经营", "劳务工队", "物流货运", "企业服务"};
    private final SparseArray<String> mSearchList = new SparseArray<>();
    private final SparseArray<String> mSearchHintList = new SparseArray<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private int mCurIndex = 0;

    public static void start(Context context) {
        Intent intent = new Intent(context, MemberBusinessActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_business_2);
        mBinding.ivBack.setOnClickListener(v -> finish());
        intTab();
        initSearchView();
        for (int i = 0; i < mTitles.length; i++) {
            if(mTitles[i].equals("租赁经营")){
                mFragments.add(BusinessLeaseFragment.newInstance());
            }else {
                mFragments.add(MemberBusinessFragment.newInstance(i));
            }
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBinding.vp.setOffscreenPageLimit(mTitles.length);
        mBinding.vp.setAdapter(mPagerAdapter);
    }


    public void intTab() {
        for (int i = 0; i < mTitles.length; i++) {
            mBinding.tab.addTab(mBinding.tab.newTab().setText(mTitles[i]));
        }
        mBinding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mCurIndex = tab.getPosition();
                mBinding.vp.setCurrentItem(mCurIndex);
                setEditContent();

                TextView textView = new TextView(getActivity());
                float selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 16,
                        getResources().getDisplayMetrics());
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,selectedSize);
                textView.setTextColor(getResources().getColor(R.color.color_white));
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                textView.setText(tab.getText());
                tab.setCustomView(textView);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mBinding.tab.setupWithViewPager(mBinding.vp);
    }



    private String mSearchText = ""; // 搜索内容

    private void setEditContent() {
        switch (mCurIndex) {
            case 0:
            case 6:
            case 3:
            case 2:
                mBinding.activityBar.ll.setVisibility(View.VISIBLE);
                String hint = mSearchHintList.get(mCurIndex);
                mBinding.activityBar.etView.setHint(hint);
                break;
            case 1:
            case 4:
            case 5:
                mBinding.activityBar.ll.setVisibility(View.GONE);
                break;
        }

    }

    private void initSearchView() {
        mSearchList.put(mCurIndex, "");
        mSearchHintList.put(0, "请输入企业名称");
        mSearchHintList.put(1, "请输入招标名称");
        mSearchHintList.put(2, "请输入商品或集采地或发起方");
        mSearchHintList.put(3, "请输入出租内容");
        mSearchHintList.put(4, "请输入工队名称或工程类型");
        mSearchHintList.put(5, "请输入货源信息");
        mSearchHintList.put(6, "请输入企业名称");
        setEditContent();
        String hint = mSearchHintList.get(mCurIndex);
        mBinding.activityBar.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mBinding.activityBar.etView.setHint(hint);
        mBinding.activityBar.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return true;
                    }
                    if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入不同的搜索内容");
                        return true;
                    }
                    mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                    loadSearchData();
                }
                return false;
            }
        });
        mBinding.activityBar.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || s.length() == 0) {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
                } else {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.activityBar.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchText = "";
                mBinding.activityBar.etView.setText("");
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mSearchText)) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return;
                    }
                }
                if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), "请输入不同的搜索内容");
                    return;
                }
                mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
    }

    public String getSearchText() {
        return mSearchList.get(mCurIndex);
    }

    private void loadSearchData() {
        mSearchList.put(mCurIndex, mSearchText);
        Fragment fragment =mFragments.get(mBinding.tab.getSelectedTabPosition());
        if(fragment instanceof MemberBusinessFragment){
            ((MemberBusinessFragment) fragment).loadData();
        }
        if (fragment instanceof BusinessLeaseFragment) {
            ((BusinessLeaseFragment) fragment).loadData(false);
        }
    }

    /**
     * 子Fragment调用父Activity中的方法设置搜索显示问题
     * @param editTextHint
     */
    public void setHintText(String editTextHint) {
        if (!TextUtils.isEmpty(editTextHint)){
            mBinding.activityBar.etView.setHint(editTextHint);
        }
    }
}
